#include <QFile>
#include <QDebug>
#include <QUdpSocket>
#include "processes.h"
#include "macro.h"

Processes::Processes(QByteArray parAdapName,quint16 parPort,QObject *parent) :
    QProcess(parent)
{
    adapterName=parAdapName;
    udpPort=parPort;
    udpSocket=new QUdpSocket(this);

    connect(this,SIGNAL(error(QProcess::ProcessError)),this,SLOT(slotError(QProcess::ProcessError)));
    connect(this,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(slotFinished(int,QProcess::ExitStatus)));
    connect(this,SIGNAL(started()),this,SLOT(slotStarted()));
    connect(this,SIGNAL(stateChanged(QProcess::ProcessState)),this,SLOT(slotStateChanged(QProcess::ProcessState)));
}

void Processes::slotError(QProcess::ProcessError errorCode)
{
    switch(errorCode)
    {
        case 0:
                emit sigProcessError(adapterName,"FailedToStart");
        break;
        case 1:
                emit sigProcessError(adapterName,"Crashed");
                break;
        case 2:
                emit sigProcessError(adapterName,"Timedout");
                break;
        case 3:
                emit sigProcessError(adapterName,"ReadError");
                break;
        case 4:
                emit sigProcessError(adapterName,"WriteError");
                break;
        case 5:
                emit sigProcessError(adapterName,"UnknownError");
                break;
        default:
                emit sigProcessError(adapterName,"ErrorCode Unexpected");

    }
}

void Processes::slotFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
    switch(exitStatus)
    {
        case 0:
            emit sigProcessFinished(adapterName,exitCode,"NormalExit");
            break;
        case 1:
            emit sigProcessFinished(adapterName,exitCode,"CrashExit");
            break;
        default:
            emit sigProcessFinished(adapterName,exitCode,"ExitStatus Unexpected");

    }
}

void Processes::slotStarted()
{
    emit sigProcessStarted(adapterName);
}

void Processes::slotStateChanged(QProcess::ProcessState pstate)
{
    switch(pstate)
    {
    case 0:
        emit sigProcessStateChanged(adapterName,"NotRunning");
        break;
    case 1:
        emit sigProcessStateChanged(adapterName,"Starting");
        break;
    case 2:
        emit sigProcessStateChanged(adapterName,"Running");
        break;
    default:
        emit sigProcessStateChanged(adapterName,"Processsate UnExpected");

    }
}

void Processes::slotProcessHide(QByteArray processName)
{
    if(processName!=adapterName)
        return;
    else
    {
        udpSocket->writeDatagram("Signal Hide",QHostAddress(aServerIP),udpPort);
    }
}

void Processes::slotProcessShow(QByteArray processName)
{
    if(processName!=adapterName)
        return;
    else
    {
        if(state()==QProcess::NotRunning)
        {
            emit sigErrorInfo(adapterName+" is not running. Starting it...");
            start(adapterName);
        }
        udpSocket->writeDatagram("Signal Show",QHostAddress(aServerIP),udpPort);

    }
}

Processes::~Processes()
{
    qDebug()<<"One process destroyed";
}
